package com.example.springsecurity.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springsecurity.domain.Echart;
import com.example.springsecurity.security.SecurityUtils;
import com.example.springsecurity.service.ProjectCountEchartsService;
import com.example.springsecurity.utils.ResultBean;
import com.example.springsecurity.utils.ifnull.StringUtils;
import com.example.springsecurity.utils.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ProjectCountEchartsController
 * @Description TODO     项目统计
 * @Author lvyongwei
 * @Date 2020-10-20 9:16
 **/
@RestController
@RequestMapping("/projectCount")
public class ProjectCountEchartsController {

    @Autowired
    private ProjectCountEchartsService projectCountEchartsService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取学院顶部数据
     * @return
     */
    @PostMapping("/getTopPage")
    public ResultBean getTopPage(){
        /**
         * 获取项目数量
         */
        List<Echart> echarts = projectCountEchartsService.getAllProject(null);
        Integer projectCount = echarts.size();
        /**
         * 获取覆盖学院个数
         */
        List<Map<String,Object>> colleges = projectCountEchartsService.getCollegeDistribute();
        Integer collegeCount = colleges.size();
        /**
         * 获取覆盖专业的个数
         */
        List<Map<String,Object>> majors = projectCountEchartsService.getMajorDistribute();
        Integer majorCount = majors.size();
        /**
         * 获取总学习人次  布置的总人数 + 选修提交的记录人数 + 体验人次
         */
        Integer totalLearning = projectCountEchartsService.getAllAssign() + projectCountEchartsService.getAllShare();
        /**
         * 获取总时长
         */
        Double totalHour = projectCountEchartsService.getAllAssignHour() + projectCountEchartsService.getAllShareHour();
        /**
         * 获取注册人数
         */
        Integer registerCount = projectCountEchartsService.getAllRegister();
        ResultBean resultBean = new ResultBean();
        resultBean.put("projectCount",projectCount);
        resultBean.put("collegeCount",collegeCount);
        resultBean.put("majorCount",majorCount);
        resultBean.put("totalLearning",totalLearning);
        resultBean.put("totalHour",totalHour);
        resultBean.put("registerCount",registerCount);
        resultBean.put("code",200);
        resultBean.put("msg","获取数据成功");
        return resultBean;
    }

    /**
     * 获取学院分布
     * @return
     */
    @PostMapping("/collegeDistribute")
    public ResultBean getCollegeDistribute(){
        try{
            List<Map<String,Object>> map = projectCountEchartsService.getCollegeDistribute();
            return ResultBean.success("获取信息成功",map);
        }catch (Exception e){
            return ResultBean.error("获取信息失败");
        }
    }


    /**
     * 获取专业分布
     * @return
     */
    @PostMapping("/majorDistribute")
    public ResultBean getMajorDistribute(){
        try{
            List<Map<String,Object>> map = projectCountEchartsService.getMajorDistribute();
            return ResultBean.success("获取信息成功",map);
        }catch (Exception e){
            return ResultBean.error("获取信息失败");
        }
    }

    /**
     * 校内教学情况
     * @param data    {"code":1,schoolYear:""}
     *           code  1,2 为1时获取布置轮次,为2时获取使用人数
     * @return
     */
    @PostMapping("/schoolTeaching")
    public ResultBean getSchoolTeaching(@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer code = jsonObject.getInteger("code");
        String schoolYear = jsonObject.getString("schoolYear");
        //判断为获取校内教学的布置轮次
        if(!StringUtils.isNull(code) && code == 1){
            try{
                List<Map<String,Object>> maps = projectCountEchartsService.getAssignCount(schoolYear);
                if(maps.size() == 0){
                    return ResultBean.error("当前年份无布置");
                }
                /**
                 * 应前端要求定义两集合
                 */
                List<String> keys = new ArrayList<>();
                List<Integer> values = new ArrayList<>();
                for(Map<String,Object> map : maps){
                    values.add((Integer) ((Long) map.get("count")).intValue());
                    keys.add((String) map.get("projectName"));
                }
                ResultBean resultBean = new ResultBean();
                resultBean.put("keys",keys);
                resultBean.put("values",values);
                resultBean.put("code",200);
                resultBean.put("msg","获取数据成功");
                return resultBean;
            }catch (Exception e){
                e.printStackTrace();
                return ResultBean.error("获取数据失败");
            }
        }else if(!StringUtils.isNull(code) && code == 2){
            try{
                List<Map<String,Object>> maps = projectCountEchartsService.getAssignStudentCount(schoolYear);
                if(maps.size() == 0){
                    return ResultBean.error("当前年份无布置");
                }
                /**
                 * 应前端要求定义两集合
                 */
                List<String> keys = new ArrayList<>();
                List<Integer> values = new ArrayList<>();
                for(Map<String,Object> map : maps){
                    values.add((Integer) ((Long) map.get("count")).intValue());
                    keys.add((String) map.get("projectName"));
                }
                ResultBean resultBean = new ResultBean();
                resultBean.put("keys",keys);
                resultBean.put("values",values);
                resultBean.put("code",200);
                resultBean.put("msg","获取数据成功");
                return resultBean;
            }catch (Exception e){
                return ResultBean.error("获取数据失败");
            }
        }else{
            return ResultBean.error("获取数据失败");
        }
    }


    /**
     * 共享统计情况
     * @param data    {
     *                "ranking":3,  排名
     *                "time":"",
     *                "according":""   1.点击量 2.学习人次 3.完成人次 4.学习时长
     *                }
     *           code  1,2 为1时获取布置轮次,为2时获取使用人数
     * @return
     */
    @PostMapping("/projectShare")
    public ResultBean getProjectShare(@RequestBody String data) {
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer ranking = jsonObject.getInteger("ranking");
        Date time = jsonObject.getDate("time");
        Integer according = jsonObject.getInteger("according");
        if(StringUtils.isNull(according)){
            return ResultBean.error("获取信息失败");
        }
        if(!StringUtils.isNull(according) && according==1){
            try{
                List<Echart> echarts = projectCountEchartsService.getClickCount(ranking);
                return ResultBean.success("获取数据成功",echarts);
            }catch (Exception e){
                return ResultBean.error("获取数据失败");
            }
        }
        if(!StringUtils.isNull(according) && according==2){
            try{
                List<Echart> echarts = projectCountEchartsService.getStudyCount(ranking,time);
                return ResultBean.success("获取数据成功",echarts);
            }catch (Exception e){
                e.printStackTrace();
                return ResultBean.error("获取数据失败");
            }
        }
        if(!StringUtils.isNull(according) && according==3){
            try{
                List<Echart> echarts = projectCountEchartsService.getFinishCount(ranking,time);
                return ResultBean.success("获取数据成功",echarts);
            }catch (Exception e){
                return ResultBean.error("获取数据失败");
            }
        }
        if(!StringUtils.isNull(according) && according==4){
            try{
                List<Echart> echarts = projectCountEchartsService.getStudyTime(ranking,time);
                return ResultBean.success("获取数据成功",echarts);
            }catch (Exception e){
                return ResultBean.error("获取数据失败");
            }
        }
        return ResultBean.error("获取信息失败");
    }

    /**
     * 获取实验项目
     * @param data {"kind": 1 }   共享传1  其他传null
     * @return
     */
    @PostMapping("/getProject")
    public ResultBean getProject(@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer kind = jsonObject.getInteger("kind");
        try{
            List<Echart> echarts = projectCountEchartsService.getAllProject(kind);
            return ResultBean.success("获取数据成功",echarts);
        }catch (Exception e){
            return ResultBean.error("获取数据失败");
        }
    }

    /**
     * 获取成绩分布
     * {
     *     "kind": 1 时,代表共享的,其他代表校内教学(即全部)
     *     "projectId": 1  项目实验id
     *     "time": 2020    字符串
     * }
     * @return
     */
    @PostMapping("/scoreDistribute")
    public ResultBean getScoreDistribute(@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer kind = jsonObject.getInteger("kind");
        Integer projectId = jsonObject.getInteger("projectId");
        String time = jsonObject.getString("time");
        //查看共享统计
        if(!StringUtils.isNull(kind) && kind == 1){
            try{
                List<Map<String,Object>> list = projectCountEchartsService.getScoreDistributeByShare(projectId);
                /**
                 * 前端要求格式
                 */
                String[] keys = new String[]{"0~9","10~19","20~29","30~39","40~49","50~59","60~69","70~79","80~89","90~100"};
                List<Integer> values = new ArrayList<>();
                for(int i=0;i<keys.length;i++){
                    values.add((Integer) ((Long) list.get(0).get(keys[i])).intValue());
                }
                ResultBean resultBean = new ResultBean();
                resultBean.put("keys",keys);
                resultBean.put("values",values);
                resultBean.put("code",200);
                resultBean.put("msg","获取共享列表批改实验记录成功");
                return resultBean;
            }catch (Exception e){
                e.printStackTrace();
                return ResultBean.success("暂无已批改实验记录");
            }
        } else{    //即项目教学内容(所有项目)
            Integer count = projectCountEchartsService.getAssignCountById(projectId,time);
            if(count == 0){
                return ResultBean.success("本学年无实验布置信息");
            }
            try{
                List<Map<String,Object>> list = projectCountEchartsService.getScoreDistributeAll(projectId,time);
                /**
                 * 前端要求格式
                 */
                String[] keys = new String[]{"0~9","10~19","20~29","30~39","40~49","50~59","60~69","70~79","80~89","90~100"};
                List<Integer> values = new ArrayList<>();
                for(int i=0;i<keys.length;i++){
                    values.add((Integer) ((Long) list.get(0).get(keys[i])).intValue());
                }
                ResultBean resultBean = new ResultBean();
                resultBean.put("keys",keys);
                resultBean.put("values",values);
                resultBean.put("code",200);
                resultBean.put("msg","获取共享列表批改实验记录成功");
                return resultBean;
            }catch (Exception e){
                return ResultBean.success("暂无已批改实验记录");
            }
        }
    }

    /**
     * 获取详细数据
     * @param data
     * @return
     */
    @PostMapping("/detailData")
    public ResultBean getDetailData(@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer kind = jsonObject.getInteger("kind");
        String time = jsonObject.getString("time");
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        if(StringUtils.isNull(pageNum) || pageNum == 0){
            pageNum = 0;
        }
        if(StringUtils.isNull(pageSize) || pageSize == 0){
            pageSize =10;
        }
        /**
         * 判断是获取的共享实验下的详细数据还是校内教学下的
         */
        if(!StringUtils.isNull(kind) && kind == 1){
            ResultBean jsonObjects = projectCountEchartsService.getDetailDataShare(pageNum,pageSize,null);
            return jsonObjects;
        }else{
            ResultBean jsonObjects = projectCountEchartsService.getDetailDataAll(pageNum,pageSize,time,null);
            return jsonObjects;
        }
    }


    /**
     * 获取教师/负责人负责的项目 项目统计情况
     * @param data
     * @return
     */
    @PostMapping("/detailDataByTeacher")
    public ResultBean getDetailDataByTeacher(@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer kind = jsonObject.getInteger("kind");
        String time = jsonObject.getString("time");
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        /**
         * 获取当前登录人id
         */
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        /**
         * 获取当前用户负责的实验项目,以及共享项目
         */
        //负责的项目
        List<Echart> echarts = projectCountEchartsService.getProjects(userId);
        //共享项目
        List<Echart> echartsShare = projectCountEchartsService.getProjectsShare(userId);
        if(StringUtils.isNull(pageNum) || pageNum == 0){
            pageNum = 0;
        }
        if(StringUtils.isNull(pageSize) || pageSize == 0){
            pageSize =10;
        }
        /**
         * 判断是获取的共享实验下的详细数据还是校内教学下的
         */
        if(!StringUtils.isNull(kind) && kind == 1){
            ResultBean jsonObjects = projectCountEchartsService.getDetailDataShare(pageNum,pageSize,echartsShare);
            return jsonObjects;
        }else{
            ResultBean jsonObjects = projectCountEchartsService.getDetailDataAll(pageNum,pageSize,time,echarts);
            return jsonObjects;
        }
    }
}
